package com.lyy.common.api.gaode;

import com.lyy.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 俞伟
 * @version 1.0
 * @date 2020/10/18
 * @description com.lyy.common.api.gaode.utils
 */
@Slf4j
public class GaoDeAPI {
    /**
     * 高德密钥
     */
    public static final String KEY = "0a19c21b9d8b8e9d3ddaf4fd76f6a2bf";

    /**
     * 高德api根地址
     */
    public static final String BASE_URL = "https://restapi.amap.com";

    /**
     * 通过ip获取所在地址
     */
    public static final String GET_ADDRESS_BY_IP = "/v3/ip";

    /**
     * 天气查询API
     */
    public static final String GET_WEATHER = "/v3/weather/weatherInfo";

    /**
     * 日期转化map
     */
    public static Map<String, String> weekMap;

    /**
     * 静态代码块初始化日期转化map
     */
    static {
        weekMap = new HashMap<>();
        weekMap.put("1", "星期一");
        weekMap.put("2", "星期二");
        weekMap.put("3", "星期三");
        weekMap.put("4", "星期四");
        weekMap.put("5", "星期五");
        weekMap.put("6", "星期六");
        weekMap.put("7", "星期日");
    }

    /**
     * 返回格式
     */
    public enum ReturnFormat {
        RETURN_JSON("JSON"),
        RETURN_XML("XML"),
        BASE("base"),
        ALL("all"),
        ;
        private String value;

        ReturnFormat(String value) {
            setValue(value);
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    /**
     * IP定位API服务地址
     *
     * @param ip
     * @return
     */
    public static String getIpAddress(String ip) {
        String rs = null;
        Map<String, String> map = new HashMap<>();
        map.put("key", KEY);
        map.put("ip", ip);
        map.put("output", ReturnFormat.RETURN_JSON.getValue());
        try {
            rs = HttpUtils.sendGet(BASE_URL + GET_ADDRESS_BY_IP, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        log.info("获取ip所在地区::{}", rs);
        return rs;
    }

    /**
     * 获取天气预报
     *
     * @param code
     * @return
     */
    public static String getWeather(String code) {
        String rs = null;
        Map<String, String> map = new HashMap<>();
        map.put("key", KEY);
        map.put("city", code);
        map.put("extensions", ReturnFormat.ALL.getValue());
        map.put("output", ReturnFormat.RETURN_JSON.getValue());
        try {
            rs = HttpUtils.sendGet(BASE_URL + GET_WEATHER, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        log.info("获取地区天气::{}", rs);
        return rs;
    }

    public static void main(String[] args) {
        System.out.println(getWeather("370812"));
    }
}
